//
//  SFUnderLBoViewController.m
//  GiftThat
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "SFUnderLBoViewController.h"

#import "SFGiftOtherCellModel.h"
#import "SFSelectionCellModel.h"
#import "SFGiftThatTableViewCell.h"
#import "SFJumpWebViewController.h"
@interface SFUnderLBoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSString *string;
@property (nonatomic,strong) NSString *str2;
@property (nonatomic,strong)  NSMutableString *mutableString;
@property (nonatomic,strong) SFGiftOtherCellModel *GiftOtherCellModel;
@property (nonatomic,strong) SFSelectionCellModel *SelectionCellModel;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) NSString *cellString;

@end

@implementation SFUnderLBoViewController
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithURL:(NSString *)url
{
    self = [super init];
    if (self) {
        self.string = url;
   

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStyleDone target:self action:@selector(leftButton:)];
     NSString *str1 = @"liwushuo:///page?type=url&url=https%3A%2F%2Fevent.liwushuo.com%2Ftopics%2Fdaily-lucky";

   NSString *string = @"liwushuo:///page?type=post&post_id=1031070&page_action=navigation";
    self.str2 = [string substringToIndex:35];

    if ([self.string isEqualToString:str1]) {
        
        NSString *str = @"http://www.liwushuo.com/posts/1014961/content";
            NSURL *url = [NSURL URLWithString:str];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 50)];
            [webview loadRequest:request];
            [self.view addSubview:webview];
    }else{
        NSString *string = [self.string substringToIndex:35];
    if ([string isEqualToString:self.str2]) {
     
        NSRange range = {35,7};
        NSString *string = [self.string substringWithRange:range];
        
        NSString *str = @"http://www.liwushuo.com/posts/1031070/content";
        
        NSMutableString *MutableString = [[NSMutableString alloc]initWithString:str];
        NSRange newrange = {30,7};
        [MutableString replaceCharactersInRange:newrange withString:string];
       
        NSURL *url = [NSURL URLWithString:MutableString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 50)];
        [webview loadRequest:request];
        [self.view addSubview:webview];
        
    }else{
        NSString *str3 = [self.string substringFromIndex:37];
        NSString *string = @"http://api.liwushuo.com/v2/collections/187/posts?limit=20&offset=0";
        self.mutableString = [[NSMutableString alloc]initWithString:string];
        NSRange range = {39,3};
        [self.mutableString replaceCharactersInRange:range withString:str3];
        
        [self GetData];
        [self GetTableView];
        self.dataSource = [NSMutableArray array];
    }
        
        
    }
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        self.tableview.backgroundColor = [UIColor colorWithRed:37 / 255.0 green:36 / 255.0 blue:40 / 255.0 alpha:1.0];
    }else{
        self.tableview.backgroundColor = [UIColor whiteColor];
    }
    
    
}
-(void)GetData{
 
    [CXNetWorkTool getWithUrl:self.mutableString parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *array = [NSArray arrayWithArray:[[result objectForKey:@"data"] objectForKey:@"posts"]];
        self.cellString = [[[result objectForKey:@"data"] objectForKey:@"paging"] objectForKey:@"next_url"];
       
        for (NSDictionary *dic in array) {
            SFSelectionCellModel *data = [[SFSelectionCellModel alloc]initWithDictionary:dic];
            
            [self.dataSource addObject:data];
          
        }
        [self.tableview reloadData];
       
     
    } fail:^(NSError *result) {
        
    }];
}

//创建ableView
- (void) GetTableView{
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, - 35, self.view.width, self.view.height - 35) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.tableview registerClass:[SFGiftThatTableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [self.view addSubview:self.tableview];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SFGiftThatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
    if (self.dataSource.count > indexPath.row) {
        
        [cell setSelectionCellModel:self.dataSource[indexPath.row]];
    }
   
    return cell;
    
    
}
//tableview点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
   NSString *url = [self.dataSource[indexPath.row] content_url];
    SFJumpWebViewController *JumpWebView = [[SFJumpWebViewController alloc]initWithURL:url];
    [self.navigationController pushViewController:JumpWebView animated:YES];
  
    
}

- (void)leftButton:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    


//    NSURL *url = [NSURL URLWithString:self.string];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    UIWebView *webview = [[UIWebView alloc]initWithFrame:self.view.bounds];
//    [webview loadRequest:request];
//    [self.view addSubview:webview];
    
    
    
}
- (void) changeDay:(NSNotification *)message {
    if ([message.object isEqualToString:@"BlackColor"]) {
        self.tableview.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];
    }else {
        self.tableview.backgroundColor = [UIColor whiteColor];
    }
    [self.tableview reloadData];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
